package com.group2.view;
import static model.netBase.IP;
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.net.SocketException;

import javax.swing.*;
import javax.swing.border.EmptyBorder;

import static java.awt.Image.SCALE_DEFAULT;
import model.*;
/*主界面：学生视图
 * 功能：
 * 1.个人学籍信息
 * 2.选课系统
 * 3.图书馆
 * 4.校园商店
 * 5.校园银行
 * 6.用户密码修改
 * */

public class UserMainFrame_Student extends javax.swing.JFrame implements ActionListener {

    /* 检测打开了哪些窗口，退出登录时统一销毁 */
    private UserLogOnFrame Logon;// 返回登录
    private UserPasswordModifyFrame Userpwd;// 密码修改
    private ShopMainFrame_User ShopStudent;// 商店
    private CourseMainViewFrame_Student CourseStudent;// 课程
    private StudentMainFrame_User StudentUser;// 学籍
    private BankMainFrame_Student BankStudent;//银行
    private LibraryMainFrame_Student LibraryStudent;// 图书馆

    private Timer time;
    private JButton jb_student;
    private JButton jb_course;
    private JButton jb_library;
    private JButton jb_store;
    private JButton jb_bank;
    private JButton jb_user;
    private JButton jb_exit;

    private JLabel jlabel_1;
    private JLabel jlabel_2;
    private JLabel jlabel_3;
    private JLabel jlabel_4;
    private JLabel jlabel_5;
    private JLabel jlabel_6;
    private JLabel jlabel_main;

    private JPanel contentPane;

    private String currentName;//当前用户名

    public UserMainFrame_Student(String name) {
        currentName = name;
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        initComponents();
        Font font = new Font("微软雅黑", Font.PLAIN, 14);
        java.util.Enumeration keys = UIManager.getDefaults().keys();
        while (keys.hasMoreElements()) {
            Object key = keys.nextElement();
            Object value = UIManager.get(key);
            if (value instanceof javax.swing.plaf.FontUIResource) {
                UIManager.put(key, font);
            }
        }
        new ToolTip().setToolTip(new javax.swing.ImageIcon(UserLogOnFrame.class.getResource("/images/kefu.png")),
                " 亲爱的同学，欢迎来到虚拟校园！\n\n 我是您真正人性化的智能助手Q宝！");
        time = new Timer(30000, (ActionListener) this);
        time.start();
        initComponents();
        setBounds(100, 200, 900, 700);
        this.setLocationRelativeTo(null);
        this.addMouseListener(new MouseListener() {@Override public void mouseClicked(MouseEvent e) {}@Override public void mousePressed(MouseEvent e) {}@Override public void mouseReleased(MouseEvent e) {}@Override public void mouseEntered(MouseEvent e) {time.stop();}@Override public void mouseExited(MouseEvent e) {time.start();}});

    }

    private void initComponents() {

        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        jb_exit = new JButton("退出登录");
        jb_exit.setIcon(new ImageIcon(UserLogOnFrame.class.getResource("/images/exit.png")));
        jb_exit.setBounds(710, 20, 150, 28);
        jb_exit.setBorderPainted(false);
        jb_exit.setFocusPainted(false);
        jb_exit.setContentAreaFilled(false);
        contentPane.add(jb_exit);
        jb_exit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                ExitActionPerformed();
            }
        });

        setTitle("欢迎来到虚拟校园系统！Welcome to vCampus-Student");

        jlabel_main = new JLabel();
        jlabel_main.setText("虚拟校园系统 · 学生主界面");
        jlabel_main.setIcon(new ImageIcon(UserLogOnFrame.class.getResource("/images/campus.png")));
        jlabel_main.setFont(new Font("微软雅黑", 1, 17));
        jlabel_main.setIconTextGap(15);
        jlabel_main.setBounds(303, 58, 339, 40);
        contentPane.add(jlabel_main);

        jb_student = new JButton();
        jb_student.setBounds(143, 110, 179, 208);
        jb_student.setBorderPainted(false);
        jb_student.setContentAreaFilled(false);

        //学籍
        jlabel_1 = new JLabel();
        //jlabel_1.setText("我的学籍");
        jlabel_1.setBounds(193, 270, 69, 30);
        //jb_student.setFocusPainted(false);
        jb_student.setIcon(new ImageIcon(UserLogOnFrame.class.getResource("/images/1.png")));
        contentPane.add(jlabel_1);
        contentPane.add(jb_student);
        jb_student.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                jb_student.setBounds(143, 110, 179, 208);
                jb_student.setBorderPainted(true);
                jb_bank.setBorderPainted(false);
                jb_course.setBorderPainted(false);
                jb_library.setBorderPainted(false);
                jb_store.setBorderPainted(false);
                jb_user.setBorderPainted(false);
                StudentActionPerformed();
            }@Override public void mousePressed(MouseEvent e) {}@Override public void mouseReleased(MouseEvent e) {}
            @Override
            public void mouseEntered(MouseEvent e) {
                jb_student.setBorderPainted(false);
                jb_student.setBounds(143, 95, 179, 208);
            }

            @Override
            public void mouseExited(MouseEvent e) {
                jb_student.setBounds(143, 110, 179, 208);
            }
        });
        //选课
        jb_course = new JButton();
        //jlabel_2 = new JLabel();
        jb_course.setBounds(389, 110, 147, 209);
        //jlabel_2.setBounds(434, 270, 63, 30);
        jb_course.setBorderPainted(false);
        jb_course.setContentAreaFilled(false);
        //jb_course.setFocusPainted(false);
        jb_course.setIcon(new ImageIcon(UserLogOnFrame.class.getResource("/images/2.png")));
        //contentPane.add(jlabel_2);
        contentPane.add(jb_course);
        jb_course.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                jb_course.setBounds(389, 110, 147, 209);
                jb_course.setBorderPainted(true);
                jb_student.setBorderPainted(false);
                jb_bank.setBorderPainted(false);
                jb_library.setBorderPainted(false);
                jb_store.setBorderPainted(false);
                jb_user.setBorderPainted(false);
                try {
                    CourseActionPerformed();
                } catch (IOException ex) {
                    throw new RuntimeException(ex);
                } catch (ClassNotFoundException ex) {
                    throw new RuntimeException(ex);
                }
            }@Override public void mousePressed(MouseEvent e) {}@Override public void mouseReleased(MouseEvent e) {}
            @Override
            public void mouseEntered(MouseEvent e) {
                jb_course.setBorderPainted(false);
                jb_course.setBounds(389, 95, 147, 209);
            }
            @Override
            public void mouseExited(MouseEvent e) {
                jb_course.setBounds(389, 110, 147, 209);
            }
        });
        //图书馆
        jb_library = new JButton();
        jlabel_3 = new JLabel("图书馆");
        jb_library.setBounds(636, 110, 149, 212);
        jb_library.setBorderPainted(false);
        jb_library.setContentAreaFilled(false);
        //jb_library.setFocusPainted(false);
        jb_library.setIcon(new ImageIcon(UserLogOnFrame.class.getResource("/images/3.png")));
        contentPane.add(jb_library);
        jb_library.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                jb_library.setBounds(636, 110, 149, 212);
                jb_library.setBorderPainted(true);
                LibraryActionPerformed();
                jb_student.setBorderPainted(false);
                jb_bank.setBorderPainted(false);
                jb_course.setBorderPainted(false);
                jb_store.setBorderPainted(false);
                jb_user.setBorderPainted(false);

            }@Override public void mousePressed(MouseEvent e) {}@Override public void mouseReleased(MouseEvent e) {}
            @Override
            public void mouseEntered(MouseEvent e) {
                jb_course.setBorderPainted(false);
                jb_library.setBounds(636, 95, 149, 212);
            }
            @Override
            public void mouseExited(MouseEvent e) {
                jb_library.setBounds(636, 110, 149, 212);
            }
        });
        //校园商店
        jb_store = new JButton();
        jlabel_4 = new JLabel("校园商店");
        jb_store.setBounds(143, 376, 184, 235);
        jb_store.setBorderPainted(false);
        //jb_store.setFocusPainted(false);
        jb_store.setContentAreaFilled(false);

        String str="/images/"+"4"+".png";
        jb_store.setIcon(new ImageIcon(UserLogOnFrame.class.getResource(str)));
        //contentPane.add(jlabel_4);
        contentPane.add(jb_store);
        jb_store.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                jb_store.setBounds(143, 376, 184, 235);
                jb_store.setBorderPainted(true);
                try {
                    StoreActionPerformed();
                } catch (SocketException ex) {
                    throw new RuntimeException(ex);
                }
                jb_student.setBorderPainted(false);
                jb_bank.setBorderPainted(false);
                jb_course.setBorderPainted(false);
                jb_library.setBorderPainted(false);
                jb_user.setBorderPainted(false);

            }@Override public void mousePressed(MouseEvent e) {}@Override public void mouseReleased(MouseEvent e) {}
            @Override
            public void mouseEntered(MouseEvent e) {
                jb_store.setBorderPainted(false);
                jb_store.setBounds(143, 360, 184, 235);
            }
            @Override
            public void mouseExited(MouseEvent e) {
                jb_store.setBounds(143, 376, 184, 235);
            }
        });

        //我的银行
        jb_bank = new JButton();
        jlabel_5 = new JLabel("我的银行");
        jb_bank.setBounds(389, 390, 164, 207);
        //jb_bank.setFocusPainted(false);
        jb_bank.setBorderPainted(false);
        jb_bank.setContentAreaFilled(false);
        jb_bank.setIcon(new ImageIcon(UserLogOnFrame.class.getResource("/images/5.png")));
        //contentPane.add(jlabel_5);
        contentPane.add(jb_bank);
        jb_bank.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                jb_bank.setBounds(389, 400, 164, 207);
                jb_bank.setBorderPainted(true);
                try {
                    BankActionPerformed();
                } catch (IOException ex) {
                    throw new RuntimeException(ex);
                } catch (ClassNotFoundException ex) {
                    throw new RuntimeException(ex);
                }
                jb_student.setBorderPainted(false);
                jb_course.setBorderPainted(false);
                jb_library.setBorderPainted(false);
                jb_store.setBorderPainted(false);
                jb_user.setBorderPainted(false);

            }@Override public void mousePressed(MouseEvent e) {}@Override public void mouseReleased(MouseEvent e) {}
            @Override
            public void mouseEntered(MouseEvent e) {
                jb_bank.setBorderPainted(false);
                jb_bank.setBounds(389, 385, 164, 207);
            }
            @Override
            public void mouseExited(MouseEvent e) {
                jb_bank.setBounds(389, 400, 164, 207);
            }
        });

        //密码修改
        jb_user = new JButton();
        jb_user.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            }
        });
        jlabel_6 = new JLabel("密码修改");
        jb_user.setBounds(636, 394, 163, 203);
        jb_user.setBorderPainted(false);
        jb_user.setContentAreaFilled(false);
        jb_user.setIcon(new ImageIcon(UserLogOnFrame.class.getResource("/images/6.png")));
        contentPane.add(jb_user);
        jb_user.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                jb_user.setBounds(636, 394, 163, 203);
                jb_user.setBorderPainted(true);
                UserActionPerformed();
                jb_student.setBorderPainted(false);
                jb_bank.setBorderPainted(false);
                jb_course.setBorderPainted(false);
                jb_library.setBorderPainted(false);
                jb_store.setBorderPainted(false);
            }@Override public void mousePressed(MouseEvent e) {}@Override public void mouseReleased(MouseEvent e) {}
            @Override
            public void mouseEntered(MouseEvent e) {
                jb_user.setBorderPainted(false);
                jb_user.setBounds(636, 378, 163, 203);
            }
            @Override
            public void mouseExited(MouseEvent e) {
                jb_user.setBounds(636, 394, 163, 203);
            }
        });


    }

    // 学籍管理
    private void StudentActionPerformed() {
        StudentUser = new StudentMainFrame_User(currentName,time);
        StudentUser.setVisible(true);
        new ToolTip().setToolTip(new javax.swing.ImageIcon(UserLogOnFrame.class.getResource("/images/kefu.png")), 
				" 同学，你不会记不住自己的学籍信息吧！\n\n Q宝给你加油！");
    }

    // 选课系统
    private void CourseActionPerformed() throws IOException, ClassNotFoundException {
        CourseStudent = new CourseMainViewFrame_Student(currentName,time);
        CourseStudent.setVisible(true);
        new ToolTip().setToolTip(new javax.swing.ImageIcon(UserLogOnFrame.class.getResource("/images/kefu.png")), 
				" 强大的选课模块为你的大学保驾护航！\n\n Q宝给你加油！");
    }

    // 图书馆
    private void LibraryActionPerformed() {
        LibraryStudent = new LibraryMainFrame_Student(currentName,time);
        LibraryStudent.setVisible(true);
        new ToolTip().setToolTip(new javax.swing.ImageIcon(UserLogOnFrame.class.getResource("/images/kefu.png")), 
				" 读书破万卷，下笔如有神！\n\n Q宝给你加油！");
    }

    // 商店
    private void StoreActionPerformed() throws SocketException {
        ShopStudent = new ShopMainFrame_User(currentName,time);
        ShopStudent.setVisible(true);
        new ToolTip().setToolTip(new javax.swing.ImageIcon(UserLogOnFrame.class.getResource("/images/kefu.png")), 
				" 同学，理智剁手！\n\n Q宝给你加油！");
    }

    // 银行
    private void BankActionPerformed() throws IOException, ClassNotFoundException {
        BankStudent = new BankMainFrame_Student(currentName,time);
        BankStudent.setVisible(true);
        new ToolTip().setToolTip(new javax.swing.ImageIcon(UserLogOnFrame.class.getResource("/images/kefu.png")), 
				" 郑重提醒：无论以何种理由要求你把资金打入陌生人账户、安全账户的行为都是电信诈骗犯罪。切勿上当受骗!");
    }


    // 用户管理：密码修改
    private void UserActionPerformed() {
        Userpwd = new UserPasswordModifyFrame(currentName,time);
        Userpwd.setVisible(true);
        new ToolTip().setToolTip(new javax.swing.ImageIcon(UserLogOnFrame.class.getResource("/images/kefu.png")), 
				" 同学，你不会忘了自己的密码吧！\n\n Q宝给你加油!");
        
    }

    // 退出登录
    private void ExitActionPerformed() {
        this.dispose();
        time.stop();
        if (CourseStudent instanceof CourseMainViewFrame_Student) {
            CourseStudent.dispose();
        }
        if (LibraryStudent instanceof LibraryMainFrame_Student) {
            LibraryStudent.dispose();
        }
        if (ShopStudent instanceof ShopMainFrame_User) {
            ShopStudent.dispose();
        }
        if (BankStudent instanceof BankMainFrame_Student) {
            BankStudent.dispose();
        }
        if (StudentUser instanceof StudentMainFrame_User) {
            StudentUser.dispose();
        }
        if (Userpwd instanceof UserPasswordModifyFrame) {
            Userpwd.dispose();
        }
        new UserLogOnFrame().setVisible(true);
    }


    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new UserMainFrame_Student("09020324").setVisible(true);
                new ToolTip().setToolTip(new javax.swing.ImageIcon(UserLogOnFrame.class.getResource("/images/kefu.png")), 
						" 亲爱的同学，欢迎来到虚拟校园！\n\n 我是您真正人性化的智能助手Q宝！");
            }
        });
    }
    public void actionPerformed(ActionEvent e)
    {
        this.ExitActionPerformed();
        JOptionPane.showMessageDialog(null,"您已长时间未进行任何操作");
    }

}